package com.controller;

import com.entity.*;
import com.service.BookFlatDetailsService;
import com.service.BookFlatService;
import com.until.Result;
import com.vo.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ fileName:BookFlatController
 * @ description:
 * @ author:dzh
 * @ createTime:2022/5/9 15:53
 * @ version:1.0.0
 */
@RestController
@RequestMapping("bookFlat")
public class BookFlatController extends BaseController {
    @Resource
    private BookFlatService bookFlatService;
    @Resource
    private BookFlatDetailsService bookFlatDetailsService;

    @PostMapping("insertOne")
    public Result insertOne(@RequestBody BookFlatDetails bookFlatDetails, HttpSession session) {
        Emp emp = getSession(session);
        double amount = bookFlatDetails.getAddStock() * bookFlatDetails.getBook().getOutPrice();
        BookFlat bookFlat = new BookFlat(amount,
                bookFlatDetails.getAddStock(),
                emp.getStoresId(),
                bookFlatDetails.getStoresId(),
                emp.getEmpName());
        bookFlatService.insertOne(bookFlat);
        bookFlatDetails.setBfId(bookFlat.getBfId());
        bookFlatDetails.setBfMoney(amount);
        bookFlatDetails.setBookQty(bookFlatDetails.getAddStock());
        return success(bookFlatDetailsService.insertOne(bookFlatDetails));
    }

    @PostMapping("insertMore")
    public Result insertMore(@RequestBody List<BookFlatDetails> bookFlatDetailsList, HttpSession session) {
        Emp emp = getSession(session);
        double amount = 0;
        int sum = 0;
        for (BookFlatDetails bookFlatDetails : bookFlatDetailsList) {
            amount += bookFlatDetails.getAddStock() * bookFlatDetails.getBook().getOutPrice();
            sum += bookFlatDetails.getAddStock();
        }
        BookFlat bookFlat = new BookFlat(amount, sum, emp.getStoresId(),
                bookFlatDetailsList.get(0).getStoresId(), emp.getEmpName());
        bookFlatService.insertMore(bookFlat);
        for (BookFlatDetails bookFlatDetails : bookFlatDetailsList) {
            bookFlatDetails.setBfId(bookFlat.getBfId());
            bookFlatDetails.setBookQty(bookFlatDetails.getAddStock());
            bookFlatDetails.setBfMoney(
                    bookFlatDetails.getAddStock() * bookFlatDetails.getBook().getOutPrice()
            );
        }
        return success(bookFlatDetailsService.insertMore(bookFlatDetailsList));
    }
    @PostMapping("listPass")
    public Result listPass(@RequestBody Page<BookFlat> page) {
        return success(bookFlatService.listPass(page));
    }

    @PostMapping("listAudit")
    public Result listAudit(@RequestBody Page<BookFlat> page) {
        return success(bookFlatService.listAudit(page));
    }

    @GetMapping("listByBfId")
    public Result listByBfId(Integer bfId) {
        return success(bookFlatDetailsService.listByBfId(bfId));
    }

    @PostMapping("updatePass")
    public Result updatePass(@RequestBody BookFlat bookFlat,HttpSession session) {
       bookFlat.setBfAuditor(getSession(session).getEmpName());
       return success(bookFlatService.updatePass(bookFlat));
    }
    @PostMapping("updateReject")
    public Result updateReject(@RequestBody BookFlat bookFlat,HttpSession session) {
        bookFlat.setBfAuditor(getSession(session).getEmpName());
        return success(bookFlatService.updateReject(bookFlat));
    }
}
